<?php
class Rewrite {

  /**
   * Rewrite::replaceAccents()
   * On remplace tous les caracteres avec des accents par leur equivalent en minuscule sans accent.
   *
   * @param mixed $text
   * @return
   */
  public static function replaceAccents($text) {
    $trans = get_html_translation_table(HTML_ENTITIES); // Place les entitÈs dans un tableau
    $trans['Ä'] = '&euro;';
    foreach ($trans as $literal =>$entity) { // CrÈe deux tableaux, un pour la forme accentuÈe, l'autre sans accents
      if(ord($literal)>=192)// Ne concerne pas les caractËres comme les fractions, guillemets, etc...
      {
        $replace[]=substr($entity,1,1); //RÈcupËre le 'E' de la chaÓne '&Eaccute' etc.
        $search[]=$literal;
      }
    } //Renvoie la lettre accentuÈe
    return str_replace($search, $replace, $text);
  }

  /**
   * Rewrite::mbStrReplace()
   *
   * @param mixed $haystack
   * @param mixed $search
   * @param mixed $replace
   * @param integer $offset
   * @param string $encoding
   * @return
   */
  public static function mbStrReplace($haystack, $search,$replace, $offset=0,$encoding='auto') {
    $len_sch=mb_strlen($search,$encoding);
    $len_rep=mb_strlen($replace,$encoding);

    while (($offset=mb_strpos($haystack,$search,$offset,$encoding))!==false) {
      $haystack=mb_substr($haystack,0,$offset,$encoding)
        .$replace
        .mb_substr($haystack,$offset+$len_sch,1000,$encoding);
      $offset=$offset+$len_rep;
      if ($offset>mb_strlen($haystack,$encoding))break;
    }
    return $haystack;
  }


  /**
   * Rewrite::formatUrlRw()
   * Permet de passer une chaine de caractere au bon format URL rewriting.
   *
   * @param mixed $texte
   * @return
   */
  public static function formatUrlRw($texte) {
    $texte = html_entity_decode($texte, ENT_QUOTES);
    $texte = trim($texte);
    $texte = Rewrite::replaceAccents($texte);
    $texte = preg_replace("#[^A-Za-z0-9]#", " ",$texte);
    $texte = strtolower($texte);
    $texte = preg_replace("# +#"," ",$texte);
    $texte = trim($texte);
    $texte = preg_replace("#[^A-Za-z0-9]#", "-",$texte);
    return $texte;
  }

  /**
   * Rewrite::formatRercherche()
   *
   * @param mixed $texte
   * @return
   */
  public static function formatSearch($texte) {
    $texte = utf8_decode($texte);
    $texte = trim($texte);
    $texte = Rewrite::replaceAccents($texte);
    $texte = preg_replace("#[^A-Za-z0-9]#", " ",$texte);
    $texte = strtolower($texte);
    $texte = preg_replace("# +#"," ",$texte);
    $texte = trim($texte);
    return $texte;
  }

  /**
   * Rewrite::formatMediaposte()
   *
   * @param mixed $texte
   * @return
   */
  public static function formatMediaposte($texte) {
    $texte = trim($texte);
    $texte = Rewrite::replaceAccents($texte);
    $texte = preg_replace("#[^A-Za-z0-9]#", " ",$texte);
    $texte = strtoupper($texte);
    $texte = preg_replace("# +#"," ",$texte);
    $texte = trim($texte);
    return $texte;
  }

  /**
   * Rewrite::filtrerString()
   *
   * @param mixed $string
   * @return
   */
  public static function filtrerString($string) {

    // Convert it to lower and trim
    $string = strtolower($string);
    $string = trim($string);

    // Remove links and email addresses
    // match protocol://address/path/file.extension?some=variable&another=asf%
    $string = preg_replace("/\s([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%\=]*)([\s|\.|\,])/iu"," ", $string);
    // match www.something.domain/path/file.extension?some=variable&another=asf%
    $string = preg_replace("/\s(www\.[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%\=]*)([\s|\.|\,])/iu"," ", $string);
    // match name@address
    $string = preg_replace("/\s([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]*\@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})([\s|\.|\,])/iu"," ", $string);

    // Sanitise the string; remove unwanted characters
    $string = preg_replace('/\W/ui', ' ', $string);

    // Explode it into an array
    $terms = explode(' ',$string);

    // Remove any blacklist terms
    //$terms = array_filter($terms, 'remove_blacklist');

    return $terms;

  }

  /**
   * Rewrite::truncateText()
   *
   * @param mixed $text
   * @param mixed $maxLength
   * @return
   */
  public static function truncateText($text, $maxLength) {
    $paddingText = "...";
    if (strlen($text) > $maxLength) {
      $text = substr($text, 0, $maxLength-strlen($paddingText));
      $lastSpace = strrpos($text, " ");
      if ($lastSpace!==false) {
        $text = substr($text, 0, $lastSpace);
      }
      $text .= $paddingText;
    }
    return $text;
  }

  public static function onlySpaces($text) {
    return preg_replace("/\\s+/", " ", $text);
  }
}

?>